﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBanVeXe
{
    public partial class FormLienHe : Form
    {
        public SqlConnection myConnection;
        public SqlCommand myCommand;
        SqlDataAdapter myDataAdapter;
        public FormLienHe()
        {
            InitializeComponent();
        }

        private void FormLienHe_Load(object sender, EventArgs e)
        {
            LoadList(sender, e);
        }
        
        public void KetNoi()
        {
            string strconnect = @"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true";
            myConnection = new SqlConnection(strconnect);
            myCommand = myConnection.CreateCommand();
        }

        private void LoadList(object sender, EventArgs e)
        {
            KetNoi();
            myCommand.CommandType = CommandType.Text;
            myCommand.CommandText = "select  * FROM LienHe";
            try
            {
                if (this.myConnection.State == ConnectionState.Closed)
                    this.myConnection.Open();
                myDataAdapter = new SqlDataAdapter(myCommand);
                DataSet ds = new DataSet();
                myDataAdapter.Fill(ds, "LienHe");
                this.dgvListLienHe.DataSource = ds.Tables[0];
                this.myConnection.Close();
            }
            catch
            {
                MessageBox.Show("Lối kết nối");
            }
            txtNoiDung.Visible = true;
            txtTieuDe.Visible = true;
            lblNoiDung.Visible = true;
            lblTieuDe.Visible = true;
            btnOK.Visible = false;
            btnHuy.Visible = false;
            ssCount.Text = " || Số tuyến: " + (dgvListLienHe.RowCount - 1).ToString();

        }
        
       //================================================================
        private void btSearch_Click(object sender, EventArgs e)
        {
            btSearch.Enabled = false;
            txtNoiDung.Visible = false;
            txtTieuDe.Visible = false;
            lblNoiDung.Visible = false;
            lblTieuDe.Visible = false;
            MacDinh();
            btnOK.Visible = true;
            btnHuy.Visible = true;
        }

        private void FormLienHe_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            FormMain frm = new FormMain();
            frm.Show();
            this.Hide();
        }

        
        private void dgvListLienHe_Click(object sender, EventArgs e)
        {
            if (this.dgvListLienHe.SelectedRows.Count > 0)
            {
                try
                {
                    DataGridViewRow drow = this.dgvListLienHe.SelectedRows[0];
                    DataRowView row = drow.DataBoundItem as DataRowView;
                    this.txtTen.Text = row[1].ToString();
                    this.txtDiaChi.Text = row[2].ToString();
                    this.txtMail.Text = row[3].ToString();
                    this.txtTieuDe.Text = row[4].ToString();
                    this.txtNoiDung.Text = row[5].ToString();
                    //defaul
                    ssThongBao.Text = "Thông báo";
                    txtNoiDung.Visible = true;
                    txtTieuDe.Visible = true;
                    lblNoiDung.Visible = true;
                    lblTieuDe.Visible = true;
                    btnOK.Visible = false;
                    btnHuy.Visible = false;
                    btXoa.Enabled = true;
                }
                catch { }
            }
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa tuyến đi đã chọn?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                for (int i = dgvListLienHe.SelectedRows.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        string ma = dgvListLienHe.SelectedRows[i].Cells[1].Value.ToString();
                        if (this.myConnection.State == ConnectionState.Closed)
                            this.myConnection.Open();
                        myCommand.CommandText = "delete from LienHe where MaLH ='" + ma + "'";
                        myCommand.ExecuteNonQuery();
                        myConnection.Close();
                        DataGridViewRow row = this.dgvListLienHe.SelectedRows[i];
                        dgvListLienHe.Rows.Remove(row);
                    }
                    catch { MessageBox.Show("Không thể xóa vì có vé xe đã đặt tuyến đi này"); }
                }
                ssThongBao.Text = "Đã xóa thành công";
                ssCount.Text = " || Số tuyến: " + (dgvListLienHe.RowCount - 1).ToString();
                MacDinh();
            }
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            LoadList(sender,e);
            btXoa.Enabled = false;
            MacDinh();
            btSearch.Enabled = true;
        }
        private void MacDinh()
        {
            
            txtDiaChi.Text = "";
            txtMail.Text = "";
            txtTieuDe.Text = "";
            txtNoiDung.Text = "";
            txtTen.Text = "";
        }

        
        private void btnOK_Click(object sender, EventArgs e)
        {
            if ( txtMail.Text.Trim().Length == 0 && txtTen.Text.Trim().Length == 0 && txtDiaChi.Text.Trim().Length == 0)
            {
                ssThongBao.Text = "Nhập dữ liệu cần tìm";

            }
            else
            {
                
                myCommand.CommandText = "select * FROM LienHe where HoTen like N'" + "%" + txtTen.Text + "%" + "' and Email like '" + "%" + txtMail.Text + "%" + "' and DiaChi like N'" + "%" + txtDiaChi.Text + "%" + "'";
                myCommand.CommandType = CommandType.Text;
                if (this.myConnection.State == ConnectionState.Closed)
                    this.myConnection.Open();
                myDataAdapter = new SqlDataAdapter(myCommand);
                DataSet ds = new DataSet();
                myDataAdapter.Fill(ds, "LienHe");
                this.dgvListLienHe.DataSource = ds.Tables[0];
                if (dgvListLienHe.RowCount == 1)
                {
                    ssThongBao.Text = "Không tìm thấy";
                    this.myConnection.Close();

                }
                else
                    ssThongBao.Text = "Đã tìm thấy " + (dgvListLienHe.RowCount - 1).ToString() + " liên hệ";
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            btRefresh_Click(sender, e);
        }

        private void dgvListLienHe_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvListLienHe.Rows[e.RowIndex].Cells["STT"].Value = e.RowIndex + 1;
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            ReportLienHe frm = new ReportLienHe();
            frm.ShowDialog();
        }

        private void dgvListLienHe_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dgvListLienHe_Click(sender, e);
        }

       

     
    }
}
